﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STKMIS.Model;
using STKMIS.DAL;
using System.Data;

namespace STKMIS.BLL
{
    public class Sale
    {
        public Sale() { }
        /// <summary>
        /// 插入销售记录，主表和子表
        /// </summary>
        /// <param name="saleInfo"></param>
        /// <param name="lstSaleItem"></param>
        /// <returns></returns>
        public static bool InsertSaleSaleItem(SaleModel saleInfo, List<SaleItemModel> lstSaleItem, ref string msg)
        {
            bool isTrue = true;
            try
            {
                using (DBTransaction tran = new DBTransaction(SqlDBServer.SqlConnStringModelDb))
                {
                    tran.BeginTransaction();
                    //插入主表
                    int SaleID = 0;
                    isTrue = Sale.InsertSale(saleInfo, tran, ref msg, ref SaleID);
                    if (isTrue) //插入主表成功
                    {
                        foreach (SaleItemModel SaleItems in lstSaleItem)
                        {
                            SaleItems.SaleID = SaleID;
                            isTrue = SaleItem.InsertSaleItem(SaleItems, tran, ref msg);
                        }
                        if (isTrue)
                        {
                            tran.Commit();
                        }
                        else
                        {
                            isTrue = false;
                            tran.Rollback();
                        }
                    }
                }
            }
            catch (Exception)
            {
                msg = "插入失败";
            }
            msg = "插入成功";
            return isTrue;
        }

        /// <summary>
        /// 插入主表
        /// </summary>
        /// <param name="saleInfo"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool InsertSale(SaleModel saleInfo, DBTransaction tran, ref string msg, ref int SaleID)
        {
            int executedRows = 0;
            bool isTrue = true;
            try
            {
                List<SqlDBParameter> param = new List<SqlDBParameter>();

                param.Add(new SqlDBParameter("@SaleBillNo", DbType.String,ParameterDirection.Output));

                param.Add(new SqlDBParameter("@ProductSupplierID", DbType.Int32, saleInfo.ProductSupplierID));
                param.Add(new SqlDBParameter("@PurchaseSumMoney", DbType.Decimal, saleInfo.PurchaseSumMoney));
                param.Add(new SqlDBParameter("@SaleSumMoney", DbType.Decimal, saleInfo.SaleSumMoney));
                param.Add(new SqlDBParameter("@SaleSumProfit", DbType.Decimal, saleInfo.SaleSumProfit));

                param.Add(new SqlDBParameter("@SaleTime", DbType.DateTime, saleInfo.SaleTime));
                param.Add(new SqlDBParameter("@SaleUserID", DbType.Int32, saleInfo.SaleUserID));
                param.Add(new SqlDBParameter("@SaleRemark", DbType.String, saleInfo.SaleRemark));
                param.Add(new SqlDBParameter("@BillState", DbType.String, saleInfo.BillState));
               // param.Add(new SqlDBParameter("@SaleID", DbType.Int32, ParameterDirection.Output));

                executedRows = SqlDBServer.ExecuteStoredProc("proc_SaleItemInsert", param, tran.Transaction, SqlDBServer.SqlConnStringModelDb);

                if (executedRows <= 0)
                {
                    isTrue = false;
                }
                else
                {
                    SaleID = Int32.Parse(param[0].Value.ToString());
                }
            }
            catch (Exception)
            {
                msg = "插入失败";
                isTrue = false;
            }
            msg = "插入成功";
            return isTrue;
        }

        /// <summary>
        /// 根据所选产品ID获取单位>
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public static DataTable GetProductAndUnit(int ProductID)
        {
            try
            {
                List<SqlDBParameter> param = new List<SqlDBParameter>();

                param.Add(new SqlDBParameter("@ProductID", DbType.Int32, ProductID));

                DataTable dt = SqlDBServer.GetDataTableByStoredProc("proc_GetProductAndUnit", param, SqlDBServer.SqlConnStringModelDb);

                return dt;
            }
            catch
            {
                return null;
            }
        }

    }
}
